<?php
/**
* alltosun.com  联想帐号接入
* ============================================================================
* 版权所有 (C) 2009-2015 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 张元昊 (zhangyh@alltosun.com) $
* $Date: 2015-07-18 11:33:38 $
* $Id$
*/

class lenovo_widget
{
    //根据st来获取lenovo User信息，创建NBD用户
    public function create_lenovo_user($st, $userform, $callbackUrl)
    {
        //拿到realm
        $openapi = Config::get('openapi');
        $appid = $openapi['lenovo'][0];

        $graph_url = "https://passport.lenovo.com/interserver/authen/1.2/getaccountid?lpsust=".$st."&realm=".$appid;
        $data = curl_request($graph_url, 'post');
        $request = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
        if(!$request) {
            return array('info'=>'error', 'msg'=> '对不起，获取用户信息错误，请重试！');
        }
        $connect_info = (array)$request;
        if(!isset($connect_info['AccountID']) || !$connect_info['AccountID']) {
            return array('info'=>'error', 'msg'=> '对不起，授权信息错误，请重试！');
        } else {
            $connect_user = _uri('connect_lenovo', array('lenovo_id' => $connect_info['AccountID']));
            if(!$connect_user) {
                $info = array(
                    'lenovo_id' => $connect_info['AccountID'],
                    'connect_site_id' => 8,
                    'user_name'       => $connect_info['Username'],
                    'verified'        => $connect_info['verified'],
                    'st'              => $st
                );

                $user_id = _widget('openapi.lenovo')->rand_create_user($connect_info['AccountID'], $userform);
                if($user_id == 'error') {
                    return array('info'=>'error', 'msg'=> '创建用户失败');
                }
                $info['user_id'] = $user_id;
                $connect_id = _model('connect_lenovo')->create($info);
                if($connect_id) {
                    $user_state = _widget('openapi.lenovo')->user_login($user_id, $userform, $callbackUrl);
                    return $user_state;
                }
            } else {
                if(!$connect_user['user_id']) {
                    $user_id = _widget('openapi.lenovo')->rand_create_user($connect_info['AccountID'], $userform);
                    if($user_id == 'error') {
                        return array('info'=>'error', 'msg'=> '创建用户失败');
                    }
                    _model('connect_lenovo')->update($connect_user['id'], array('user_id' => $user_id, 'st' => $st));
                    $user_state = _widget('openapi.lenovo')->user_login($user_id, $userform, $callbackUrl);
                    return $user_state;
                } else {
                    _model('connect_lenovo')->update($connect_user['id'], array('st' => $st));
                    $user_state = _widget('openapi.lenovo')->user_login($connect_user['user_id'], $userform, $callbackUrl);
                    return $user_state;
                }
            }
        }
    }

    public function rand_create_user($lenovo_id, $userform)
    {
        //随机生成一个用户名
        $username = 'N'.$lenovo_id.rand(1,100);
        $hash     = user_helper::random_hash(6);
        $user_info = array(
            'user_name' => $username,
            'user_from' => $userform,
            'avatar'   => STATIC_URL.'/images/checkin_default.jpg'
            // 'password' => user_helper::md5_password('NBD'.$lenovo_id.'newbd', $hash),
            // 'hash'     => $hash
            );
        $user_id = _model('user')->create($user_info);
        if($user_id) {
            $return_info = user_helper::uc_register($username, '', '', '', '', 8);
            
            if($return_info['info'] == 'ok') {
                _model('user')->update($user_id, array('uc_id' => $return_info['uc_id']));
                _model('role_user')->create(array('role_id' => '2', 'user_id' => $user_id));
                return $user_id;
            } else {
                _model('user')->delete(array('id'=>$user_id));
                _model('role_user')->delete(array('user_id' => $user_id));
                return 'error';
            }
        }
    }

    public function user_login($user_id, $userform, $callbackUrl) {
        $user_info = _uri('user', $user_id);
        if($user_info) {
            user_helper::session_update($user_info, $userform);
            $ucenter_info = user_helper::uc_login($user_info['uc_id'], '', $callbackUrl,0,1);
            return array('info'=>'ok', 'msg'=> $ucenter_info);
        } else {
            return array('info'=>'error', 'msg'=> '登录用户信息不正确');
        }
    }
}

